/**
 * FileName: ShareController
 * Author:   Leopold
 * Date:     2018/10/1 18:08
 * Description:
 * History:
 */
package cn.mcsys.viewer.core.web.share;

import cn.mcsys.myadmin.common.config.LoadRidePointConfiguration;
import cn.mcsys.myadmin.common.utils.DateUtils;
import cn.mcsys.myadmin.common.utils.MapUtils;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirCarRecord;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.share.dto.AirCarDTO;
import cn.mcsys.myadmin.modules.share.dto.BookingPeopleDTO;
import cn.mcsys.myadmin.modules.viewer.entity.AirTickets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.geom.Point2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class ShareController {

    @Autowired
    private BackService backService;

    @RequestMapping("bookingCount")
    @ResponseBody
    public Map<String, BookingPeopleDTO> dtos() {
        List<AirCar> cars = backService.getAllCarList();
        Map<String, BookingPeopleDTO> map = new HashMap<>();
//        查询各个乘车点对应的在途数，总预约数，已达到，未送达，
        Map<String,List<AirCar>> pointCars = new HashMap<>();
//        将各个乘车点的车辆进行归类
        for (AirCar car : cars) {
            String pointName = car.getAirRidePoint().getName();
            List<AirCar> airCars = pointCars.get(pointName);
            if(airCars == null){
                List<AirCar> carList = new ArrayList<>();
                carList.add(car);
                pointCars.put(pointName,carList);
            }else{
                airCars.add(car);
            }
        }
        Set<Map.Entry<String, List<AirCar>>> entries = pointCars.entrySet();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map.Entry<String, List<AirCar>> entry : entries) {
            BookingPeopleDTO bookingPeopleDTO = new BookingPeopleDTO();
            map.put(entry.getKey(),bookingPeopleDTO);
            List<AirCar> carsList = entry.getValue();
            AirRidePoint airRidePoint = null;
            for (AirCar airCar : carsList) {
                airRidePoint = airCar.getAirRidePoint();
                if (airCar.getStatus() != null){
                    if(airCar.getStatus() == AirCar.RIDING){
//                    查询该车辆最新的发车记录(当天)
                        AirCarRecord airCarRecord = backService.selectLatestCarRecord(airCar,AirCar.RIDING);
                        if(airCarRecord != null){
                            int passengerSum = bookingPeopleDTO.getRidingSum()+ airCarRecord.getPassengerSum();
                            bookingPeopleDTO.setRidingSum(passengerSum);
                        }
                    }
                    if(airCar.getStatus() == AirCar.ARRIVE){
//                    查询该车辆最新的发车记录
                        AirCarRecord airCarRecord = backService.selectLatestCarRecord(airCar,AirCar.ARRIVE);
                        if(airCarRecord != null){
                            int passengerSum = bookingPeopleDTO.getArriveSum()+ airCarRecord.getPassengerSum();
                            bookingPeopleDTO.setArriveSum(passengerSum);
                        }
                    }
                }

            }
//            当天总预约人数
            int bookingSum = backService.selectBookingSumByPointId(airRidePoint.getId());
            int notArriveSum = bookingSum - bookingPeopleDTO.getArriveSum() - bookingPeopleDTO.getRidingSum();
            bookingPeopleDTO.setNotArriveSum(notArriveSum);
            bookingPeopleDTO.setBookingSum(bookingSum);
            bookingPeopleDTO.setDate(format.format(new Date()));
        }

        return map;
    }

    /**
     * 获取票务数据
     * @param beginDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    @RequestMapping("getTickets")
    @ResponseBody
    public List<AirTickets> getTickets(String beginDate,String endDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = format.parse(beginDate);
        Date end = format.parse(endDate);
        List<AirTickets> tickets = backService.selectTickets(begin,end);
        return tickets;
    }

    @RequestMapping("getCars")
    @ResponseBody
    public List<AirCarDTO> getCars(){
        return backService.trafficList();
    }

    /**
     * 判断当前车辆处在什么状态下
     *
     * @param dto
     * @param car
     */
//    private void carInWhere(BookingPeopleDTO dto, AirCar car) {
////        判断是否在乘车点
////        判断是否在线路中
////        判断是否在航展馆
//        Double lon = Double.valueOf(car.getLon());
//        Double lat = Double.valueOf(car.getLat());
////        当前时间戳
//        long currentTimeMillis = System.currentTimeMillis();
////        如果都不在则视为偏移线路
////        如果不在线路上
//        if (!isInLine(car, lon, lat, currentTimeMillis)) {
////            判断是否在乘车点上
//            if (!isInRidePoint(car, lon, lat, currentTimeMillis)) {
////                判断是否在展馆中
//                if (!inAirShowHall(car, lon, lat, currentTimeMillis)) {
////                  设置车辆为偏移线路状态
//                    car.setIsOnline(1);
//                }
//            }
//        }else{
////            设置在线路中
////            设置在线路中(添加偏移线路状态字段,上下班字段)
//            car.setIsOnline(0);
//        }
//    }

    /**
     * 判断是否在航展展馆
     *
     * @param car
     * @param lon
     * @param lat
     * @param currentTimeMillis
     */
//    private boolean inAirShowHall(AirCar car, Double lon, Double lat, long currentTimeMillis) {
//        boolean isInAirShowHall = false;
//        List<Point2D.Double> doubleList = LoadRidePointConfiguration.hangzhanguan.get(LoadRidePointConfiguration.HANGZHANGUAN);
//        if(MapUtils.IsPtInPoly(new Point2D.Double(lon,lat),doubleList)){
//            car.setStatus(AirCar.ARRIVE);
////            新增车辆状态表信息
//            backService.insertCarRecord(car,AirCar.ARRIVE);
//            backService.updateCarStatus(car.getId().longValue(),car.getStatus());
//            isInAirShowHall = true;
//        }
//        return isInAirShowHall;
//    }

    /**
     * 判断是否在乘车点上
     *
     * @param car
     * @param lon
     * @param lat
     * @param currentTimeMillis
     */
//    private boolean isInRidePoint(AirCar car, Double lon, Double lat, long currentTimeMillis) {
//        String note = car.getAirRidePoint().getNote();
////        获取该车辆对应的乘车点电子围栏
//        List<Point2D.Double> pointList = LoadRidePointConfiguration.getPointList(note);
//        boolean isInRidePoint = false;
//        if (MapUtils.IsPtInPoly(new Point2D.Double(lon, lat), pointList)) {
////                判断是否处于待发车和7-12点中
//            if (DateUtils.isInDate(currentTimeMillis, "06:30:00", "12:00:00")) {
//                if (car.getStatus() == AirCar.GO_RIDEPOINT) {
//                    car.setStatus(AirCar.WAITSTART);
////                        新增车辆状态表信息
//                    backService.insertCarRecord(car, AirCar.WAITSTART);
//                    isInRidePoint = true;
//                }
//            } else if (DateUtils.isInDate(currentTimeMillis, "13:00:00", "18:00:00")) {
//                if (car.getStatus() == AirCar.RETURNING) {
//                    car.setStatus(AirCar.RETURNED);
//                    backService.insertCarRecord(car, AirCar.RETURNED);
//                    isInRidePoint =  true;
//                }
//            }
//            backService.updateCarStatus(car.getId().longValue(),car.getStatus());
//        }
//        return isInRidePoint;
//    }

    /**
     * 判断是否在线路上
     *
     * @param car
     * @param lon
     * @param lat
     * @param currentTimeMillis
     * @return
     */
//    private boolean isInLine(AirCar car, Double lon, Double lat, long currentTimeMillis) {
//        Set<Map.Entry<String, List<Point2D.Double>>> entries = LoadRidePointConfiguration.ridePointMap.entrySet();
//        boolean isInLine = false;
//        for (Map.Entry<String, List<Point2D.Double>> entry : entries) {
////          判断是否线路中
//            if (MapUtils.IsPtInPoly(new Point2D.Double(lon, lat), entry.getValue())) {
////                判断是否处于待发车和7-12点中
//                if (DateUtils.isInDate(currentTimeMillis, "07:00:00", "12:00:00")) {
//                    if (car.getStatus() == AirCar.WAITSTART) {
//                        car.setStatus(AirCar.RIDING);
////                        新增车辆状态表信息
//                        backService.insertCarRecord(car, AirCar.RIDING);
////                        新增实时发车表信息
//                        backService.autoInsertRealTimeMessage(car, AirCar.RIDING);
//                        isInLine =  true;
//                    } else if (car.getStatus() == AirCar.ARRIVE) {
//                        car.setStatus(AirCar.AGAINSTART);
////                        更改车辆状态信息
//                        backService.insertCarRecord(car, AirCar.AGAINSTART);
//                        isInLine =  true;
//                    }
//                } else if (DateUtils.isInDate(currentTimeMillis, "13:00:00", "18:00:00")) {
//                    if (car.getStatus() == AirCar.ARRIVE) {
//                        car.setStatus(AirCar.RETURNING);
//                        isInLine =  true;
//                    } else if (car.getStatus() == AirCar.RETURNED) {
//                        car.setStatus(AirCar.AGAINRETURN);
//                        isInLine = true;
//                    }
//                }
//
//            }
//        }
//        backService.updateCarStatus(car.getId().longValue(),car.getStatus());
//        return isInLine;
//    }
}
